﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using Owl.Questionnarie.Entities;

namespace Owl.Questionnarie.DAL
{
    public class UserDAL
    {
        /// <summary>
        /// Cria ou retorna usuário já cadastrado
        /// </summary>
        /// <param name="email">email</param>
        /// <returns>UsersData</returns>
        public UsersData CreateUser(string email)
        {
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {
                    UsersData user = GetUser(email);

                    if (user == null)
                    {
                        user = new UsersData();
                        user.UserEmail = email;
                        user.UserId = Guid.NewGuid();

                        context.AddToUsersData(user);
                        context.SaveChanges();
                        return user;
                    }
                    else
                    {
                        return user;
                    }
                }

            }
            catch (Exception)
            {
                throw;
            }

        }
        
        public UsersData GetUser(string email)
        {
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {
                    UsersData user = (from u in context.UsersData
                                      where u.UserEmail == email
                                      select u).FirstOrDefault();

                    return user;

                }

            }
            catch (Exception)
            {
                throw;
            }
        }
        
        public void UpdateUser(UsersData user)
        {

            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    UsersData userData = (from u in context.UsersData
                                          where u.UserEmail == user.UserEmail
                                          select u).FirstOrDefault();

                    userData.UF = user.UF;
                    userData.Age = user.Age;
                    userData.City = user.City;
                    userData.Color = user.Color;
                    userData.Country = user.Country;
                    userData.Gender = user.Gender;
                    userData.Occupation = user.Occupation;
                    userData.Schooling = user.Schooling;

                    context.SaveChanges();

                }

            }
            catch (Exception)
            {
                throw;
            }
        }
        
        public bool ValidateUser(string email)
        {
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {


                    UsersData userData = (from u in context.UsersData
                                          where u.UserEmail == email
                                          select u).FirstOrDefault();

                    if (userData != null)
                        return true;
                    else
                        return false;
                }


            }
            catch (Exception)
            {
                throw;
            }
        }
               
        public int AnswerQuestionnarie(UsersData userData, Questionnaries questionnarie)
        {
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {
                    User_Questionnaries UserQuestionnarie = new User_Questionnaries();
                    UserQuestionnarie.UserData = userData;
                    UserQuestionnarie.Questionnarie = questionnarie;

                    context.AddToUser_Questionnaries(UserQuestionnarie);
                    return context.SaveChanges();
                }


            }
            catch (Exception)
            {
                throw;
            }
        }


        

    }
}
